//
//  CYStatus.swift
//  CYSinaWB
//
//  Created by mac on 16/2/23.
//  Copyright © 2016年 mac. All rights reserved.
//

import UIKit

class CYStatus: NSObject {
    //    微博ID  Int 和OC中 NSInteger是一样  永远匹配当前机型的位数
    var id: Int64 = 0
    //微博创建时间
    var created_at: String?
    //微博正文
    var text: String?
    //微博来源
    var source: String?
    
    //用户模型属性
    var user:CYUser?
    
    //如果是原创微博 retweeted_status不存在
    var retweeted_status: CYStatus?
    
    //配图视图数组的属性  数组  永远都不可能为nil  服务器在没有配图的情况下 依然会返回一个空数组 而非 null对象
    var pic_urls: [[String : String]]? {
        didSet {
            guard let urls = pic_urls else {
                return
            }
            
            imageUrls = [NSURL]()
            //遍历url
            for items in urls {
                let urlString = items["thumbnail_pic"]
                let str = urlString?.stringByReplacingOccurrencesOfString("thumbnail", withString: "bmiddle")
                let url = NSURL(string: str!)!
                imageUrls?.append(url)
            }
        }
    }
    //遍历后的url添加到数组
    var imageUrls: [NSURL]?
    //KVC设置初始值
    init(dict: [String : AnyObject]) {
        super.init()
        setValuesForKeysWithDictionary(dict)
    }
    
    override func setValue(value: AnyObject?, forKey key: String) {
        if key == "user" {
            if let dict = value as? [String : AnyObject] {
                user = CYUser(dict: dict)
                return
            }
        }
        if key == "retweeted_status" {
            if let dict = value as? [String : AnyObject] {
                retweeted_status = CYStatus(dict: dict)
                return
            }
        }
        super.setValue(value, forKey: key)
    }
    //过滤
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {    }

}
